﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Not3pad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.AddExtension = true;
            DialogResult result = ofd.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (TextReader reader = new StreamReader(ofd.FileName))
                {
                    textBox1.Text = reader.ReadToEnd();
                    reader.Close();
                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".txt";
            sfd.Filter = "*.txt|Text files";

            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (TextWriter writer = new StreamWriter(sfd.FileName))
                {
                    writer.Write(textBox1.Text);
                    writer.Close();
                }
            }
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            DialogResult dg = fd.ShowDialog();
            if (dg == DialogResult.OK)
            {
                textBox1.Font = fd.Font;
            }
        }

        private void printToPrinterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_currentPrintLine = 0;
            printDocument1.Print();
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_currentPrintLine = 0;
            printPreviewDialog1.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to save?", "Save?", 
                MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                saveToolStripMenuItem_Click(null, null);
            }
            this.Close();
                
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A && e.Control)
            {
                textBox1.SelectAll();
            }
        }

        private int m_currentPrintLine = 0;
        private void printDocument1_PrintPage(object sender, 
            System.Drawing.Printing.PrintPageEventArgs e)
        {
            float yPos = 0f;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            float linesPerPage = e.MarginBounds.Height / 
                textBox1.Font.GetHeight(e.Graphics);
            //m_current Print Line is a class scope variable of type int.
            while (m_currentPrintLine < textBox1.Lines.Count() 
                && count < linesPerPage)
            {
                line = textBox1.Lines[m_currentPrintLine];  //textBox1.Lines is a multi-line text box on the form.
                m_currentPrintLine++;
                yPos = topMargin + count * textBox1.Font.GetHeight(e.Graphics);
                e.Graphics.DrawString(line, textBox1.Font, Brushes.Black, 
                    leftMargin, yPos, new StringFormat());
                count++;
            }
            if (m_currentPrintLine < textBox1.Lines.Count())
            {
                e.HasMorePages = true;
            }
        }

    }
}
